﻿using System.Windows;
using System.Windows.Controls;
using System.Collections.Specialized;
using System.Configuration;
using DrCarDrive.Util;
using System.Net;
using DrCarDrive.Wpf.WpfWindow;
using System.Windows.Input;
using System;
using System.Collections.Generic;
using System.Data;
using DrCarDrive.DBService;
using DrCarDrive.Wpf.WpfPage;

namespace DrCarDrive.Wpf
{
    /// <summary>
    /// Interaction logic for DoctorLoginPage.xaml
    /// </summary>
    public partial class LoginPage : Page
    {
        private static LoginPage instance;
        public IList<string> ErrorList { get; set; }
        private DoctorCarService doctorCarService;

        public LoginPage()
        {
            InitializeComponent();
        }

        public static LoginPage Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new LoginPage();
                }

                return instance;
            }
        }

        public static void Reset()
        {
            instance = null;
        }

        private void SignInButton_Click(object sender, RoutedEventArgs e)
        {
            String userName = UserNameText.Text;
            String password = PasswordText.Password;
            ErrorLabel.Content = "";

            if (userName == "axiohelix" && password == "admn#res@xio")
            {
                try
                {
                    Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                    var settings = config.AppSettings.Settings;
                    settings["admin.password"].Value = "admin";
                    config.Save();
                    ConfigurationManager.RefreshSection("appSettings");
                    MSGWindow msg = new MSGWindow("Success", "Password Reset Successfully");
                    msg.ShowDialog();
                    UserNameText.Text = "admin";
                    PasswordText.Password = "";
                    return;
                }
                catch (Exception ex)
                {

                }
            }

            if (userName == "admin")
            {
                Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                var settings = config.AppSettings.Settings;
                if (settings["admin.password"].Value == password)
                {
                    MainWindow.Instance.Width = 1000;
                    MainWindow.Instance.TopMenu.Visibility = System.Windows.Visibility.Visible;
                    MainWindow.Instance.ContentFrame.Content = Settings.Instance;
                    MainWindow.Instance.btnHome.Visibility = System.Windows.Visibility.Hidden;
                    MainWindow.Instance.lblDrCarText.Visibility = System.Windows.Visibility.Hidden;
                    MainWindow.Instance.btnChangePassword.Visibility = System.Windows.Visibility.Visible;
                }
                else
                {
                    ErrorLabel.Content = Messages.ERR_USERNAME_OR_PASSWORD;
                }
            }
            else
            {
                doctorCarService = new DoctorCarService();
                IList<tbl_car> doctorCarList = doctorCarService.getDoctorCarListByUserNameAndPassword(userName, password);

                if (doctorCarList != null)
                {
                    if (doctorCarList.Count > 0)
                    {
                        SessionData.car = doctorCarList[0];
                        DriveService driveService = new DriveService();
                        SessionData.drive = driveService.viewByCarIDLastRecord(SessionData.car.ID);
                        if (SessionData.drive != null && SessionData.drive.STATUS == true)
                        {
                            PlaceService placeService = new PlaceService();
                            SessionData.place = placeService.viewByDriveIDLastRecord(SessionData.drive.ID);
                        }
                        MainWindow.Instance.lblCarNo.Content = SessionData.car.REG_NO;
                        MainWindow.Instance.ContentFrame.Content = DashboardPage.Instance;
                        MainWindow.Instance.TopMenu.Visibility = System.Windows.Visibility.Visible;
                        MainWindow.Instance.btnHome.Visibility = System.Windows.Visibility.Visible;
                        MainWindow.Instance.lblDrCarText.Visibility = System.Windows.Visibility.Visible;
                        MainWindow.Instance.btnChangePassword.Visibility = System.Windows.Visibility.Hidden;
                    }
                    else
                    {
                        ErrorLabel.Content = Messages.ERR_USERNAME_OR_PASSWORD;
                    }
                }
                else
                {
                    MSGWindow error = new MSGWindow(Messages.TTL_ERROR, Messages.ERR_DB);
                    error.Show();
                }
            }
        }


        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            //System.Windows.Application.Current.Shutdown();
            //UsernameText.Text = "";
            //PasswordText.Password = "";
        }

        private void EnterClicked(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Return)
            { //Keys.Enter
                e.Handled = true;
            }
        }

        private void Page_Loaded_1(object sender, RoutedEventArgs e)
        {
            MainWindow.Instance.MinWidth = 800;
            MainWindow.Instance.Width = 800;
            var screenWidth = System.Windows.SystemParameters.PrimaryScreenWidth;
            MainWindow.Instance.Left = (screenWidth - MainWindow.Instance.Width) / 2;
        }
    }
}
